﻿using Estsh.Core.Base;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Estsh.Core.Services;
using Estsh.Core.Models;


namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// 菜单业务处理类
    /// </summary>
    public class LoginService : BaseService<BaseEntity>, ILoginService
    {
        private readonly ILoginRepository repository;
        public LoginService(ILoginRepository _repository) : base(_repository)
        {
            repository = _repository;
        }
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        public List<SysEmp> RunLogin(string userNo, string userPwd, string factoryId, string factoryName)
        {
            return repository.RunLogin(userNo, userPwd, factoryId, factoryName);
        }


        /// <summary>
        /// 获取工厂信息
        /// </summary> 
        /// <returns>返回结果用动态对象接收数据</returns>
        public List<SysFactory> GetFactoryInfo()
        {
            return repository.GetFactoryInfo();
        }

        public List<SysFactory> GetFactoryInfo(string factory_id)
        {
            return repository.GetFactoryInfo(factory_id);
        }

        public List<SysEmp> GetRoleName(string loginId)
        {
            return repository.GetRoleName(loginId);
        }

        /// <summary>
        /// 通过用户账号获取用户ID与用户名称
        /// </summary>
        /// <param name="userNo"></param>
        /// <returns>返回结果用动态对象接收数据</returns>
        public List<SysEmp> GetUserInfo(string userNo)
        {
            return repository.GetUserInfo(userNo);
        }

        /// <summary>
        /// 通过用户账号获取用户ID菜单权限
        /// </summary>
        /// <param name="userID"></param>
        /// <returns>返回结果用动态对象接收数据</returns>
        public List<GetMenuList> GetRole(string userID)
        {
            List<GetMenu> getmenus = repository.GetRole(userID); 
            List<GetMenuList> menulist = new List<GetMenuList>();
            
            var typelist = getmenus.GroupBy(a => a.menuTypeName).ToList();
            for (int i = 0; i < typelist.Count; i++)
            {
                List<GetMenu> menus = getmenus.Where(a => a.menuTypeName == typelist[i].Key.ToString()).OrderBy(b =>Convert.ToInt32(b.menuSortNum)).ToList();

                GetMenuList list = new GetMenuList();
                list.menuTypeName = typelist[i].Key.ToString();
                list.menulist = menus;

                menulist.Add(list);
            }


            return menulist;
        }
    }
}